import cv2
import numpy as np
import matplotlib.pyplot as plt

correct_image = cv2.imread("./panel2.png")
correct_image = cv2.cvtColor(correct_image,cv2.COLOR_BGR2GRAY)

# 开运算 与 闭运算
# 开运算 腐蚀多余像素值
# 闭运算 膨胀特定的元素

kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))

# 闭运算
dilated_image = cv2.dilate(correct_image, kernel)
eroded_image = cv2.erode(dilated_image, kernel)

# cv2.imshow("eroded_image",eroded_image)
# cv2.waitKey(0)

#开运算
eroded_image = cv2.erode(eroded_image, kernel)
dilated_image = cv2.dilate(eroded_image, kernel)

# cv2.imshow("dilated_image",dilated_image)
# cv2.waitKey(0)

# 闭运算
dilated_image = cv2.dilate(dilated_image, kernel)
eroded_image = cv2.erode(dilated_image, kernel)

# cv2.imshow("eroded_image",eroded_image)
# cv2.waitKey(0)

# 直方图 ：确定车道线的位置
# np.sum(...,axis=0) 将一列数值相加到矩阵中
histogram = np.sum(eroded_image[:,:],axis=0)
plt.plot(np.arange(0,len(histogram)),histogram,'r-')
plt.show()

